import lookup from "./lookup";

export default function renderTmeplate(tokens, data) {
    console.log(tokens)
    let token
    let str = ''

    for(let i = 0; i < tokens.length; i++) {
        token = tokens[i]

        switch (token[0]) {
            case '#':
                let section = lookup(token[1], data)

                if(Array.isArray(section)) {
                    section.forEach(item => {
                        str += renderTmeplate(token[2], {
                            ['.']: item,
                            ...item
                        })    
                    });
                }else {
                    str += renderTmeplate(token[2], section)
                }
                break;
            case 'text':
                str += token[1]
                break;
            case 'name':
                str += lookup(token[1], data)
                break;
            default:
                break;
        }
    }

    return str
}